package com.marst.first.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marst.first.mapper.UserMapper;
import com.marst.first.bean.User;
import com.marst.first.bean.VO.Result;
import com.marst.first.utils.MD5Utils;
import com.marst.first.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class UserService {
    private static final Logger log = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;


    public Result login(String account, String password) {
        //查询数据库中是否有相对应的账号
        User dbuser = userMapper.getUserInfoAccount(account);
        //如果不存在返回错误信息到前段
        if (dbuser == null) {
            return new Result(1001, "数据不存在");
        }
        //如果存在判断密码与前端的密码是否相同
        if (StringUtils.isNotEmpty(dbuser.getPassword())) {
            boolean saltverifyMD5 = MD5Utils.getSaltverifyMD5(password, dbuser.getPassword());
            if (!saltverifyMD5) {
                //如果不同返回错误信息
                return new Result(1002, "密码错误");
            }

        }
        UUID uuid = UUID.randomUUID();
        String token = uuid.toString().replaceAll("-", "");
        redisUtils.set(account, token);
        return Result.ok(token);

    }


    public Result addUser(User user) {
        //账号不能为空
        if (StringUtils.isEmpty(user.getAccount())) {
            return new Result(1003, "账号不能为空");
        }
        //密码不能为空
        if (StringUtils.isEmpty(user.getPassword())) {
            return new Result(1003, "密码不能为空");
        }
        //姓名不能为空
        if (StringUtils.isEmpty(user.getName())) {
            return new Result(1003, "姓名不能为空");
        }
        //账号唯一性
        int userCountByAccount = userMapper.getUserCountByAccount(user.getAccount());
        if (userCountByAccount > 0) {
            return new Result(1005, "已有账号 不可重复注册");
        }
        //密码加密
        String saltMD5 = MD5Utils.getSaltMD5(user.getPassword());
        user.setPassword(saltMD5);
        user.setCreateTime(new Date());
        userMapper.insert(user);
        return Result.ok();


    }

    public Result list(JSONObject params) {

        //取参数

        String account = params.getString("account");
        String name = params.getString("name");


        //查询全部 分页查询
        Integer pageNum = params.getString("pageNum") == null ? 0 : params.getInteger("pageNum");
        Integer pageSize = params.getString("pageSize") == null ? 10 : params.getInteger("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.getUserList(account, name);
        return Result.ok(new PageInfo<>(userList));
    }


    public Result deleteUserOnLogic(int id) {
        userMapper.deleteUserOnLogic(id);
        return Result.ok();
    }

    public Result getUserById(int id) {
        return Result.ok(userMapper.getUserById(id));
    }

    public Result updateUserById(User user) {
        //账号不能为空
        if (StringUtils.isEmpty(user.getAccount())) {
            return new Result(1003, "账号不能为空");
        }
        //密码不能为空
        if (StringUtils.isEmpty(user.getPassword())) {
            return new Result(1003, "密码不能为空");
        }
        //姓名不能为空
        if (StringUtils.isEmpty(user.getName())) {
            return new Result(1003, "姓名不能为空");
        }
        //账号唯一性
        int userCountByAccount = userMapper.getUserCountForUpdate(user.getAccount(), user.getId());
        if (userCountByAccount > 0) {
            return new Result(1005, "已有账号 不可重复注册");
        }
        //密码加密
        String saltMD5 = MD5Utils.getSaltMD5(user.getPassword());
        user.setPassword(saltMD5);
        userMapper.updateById(user);
        return Result.ok();
    }
}
